﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCM.UI.ChildWindows;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Telerik.Windows.Controls;
using Baosight.ZNH.ZMCM.UI.Utils;
using System.Text;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core.ServiceModel;

namespace Baosight.ZNH.ZMCM.UI
{
    public partial class ZMCMC0 : EFViewCmes
    {
        public ZMCMC0()
        {
            InitializeComponent();
        }

        public override void Initialize(EIInfo initData)
        {
            base.Initialize(initData);
        }

        /// <summary>
        /// 发料
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnInsertButtonClick(object sender, RoutedEventArgs e)
        {
            base.OnInsertButtonClickPopup(new EIInfo(), "发料", "insert", "Export", sender, e);
        }
        /// <summary>
        /// 回收料
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnUpdateButtonClick(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            button.IsEnabled = false;
            TempButton = button;
            int count = this.grid.GetCheckedEIRows().Count;
            if (count <= 0)
            {
                this.OpenEFWindowAlert("请选择一种材料进行回收！", true);
                return;
            }
            else if (count > 1)
            {
                this.OpenEFWindowAlert("最多只能选择一种材料进行回收！", true);
                return;
            }
            else if (!this.grid.GetCheckedEIRows()[0]["fdTrunoverStatus"].ToString().Equals("发料"))
            {
                base.OpenEFWindowAlert("不能对非发料的材料进行回收！",true);
            }
            else
            {
                if (this.grid.GetCheckedEIRows()[0]["fdStatus"] != null && !this.grid.GetCheckedEIRows()[0]["fdStatus"].ToString().Equals(""))
                {
                    base.OpenEFWindowAlert("选中的记录已经回收或红冲，不能再次进行回收处理！", true);
                    return;
                }

                EFViewCmesProperty _view = new EFViewCmesProperty();
                _view.EFGridCheckedRows = this.grid.GetCheckedEIRows();
                base.ViewProperty = null;
                base.ViewProperty = _view;     //不能使用OnBeforeAction方法，在这里赋值
                IList<EIRow> _list = this.grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.grid.GetCheckedEIRows();
                EIInfo eiInfo = new EIInfo();
                EIBlock eIBlock = new EIBlock("result");
                EIRow row = new EIRow();
                if (_list.Count > 0)
                {
                    row = BaseTools.GetEIRowByOldEIRow(this.EIDataContext.GetBlock("result").GetColumns(), _list[0]);
                }
                eIBlock.SetRow(0, row);
                eiInfo.AddBlock(eIBlock);
                base.OnUpdateButtonClickPopup(eiInfo, "材料回收", "update", "Import", sender, e);
            }
        }

        /// <summary>
        /// 红冲
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnDeleteButtonClick(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            button.IsEnabled = false;
            TempButton = button;
            int count = this.grid.GetCheckedEIRows().Count;
            if (count <= 0)
            {
                this.OpenEFWindowAlert("请选择一条记录进行红冲！", true);
                return;
            }
            else if (count > 1)
            {
                this.OpenEFWindowAlert("最多只能选择一条记录进行红冲！", true);
                return;
            }
            else
            {
                if (this.grid.GetCheckedEIRows()[0]["fdTrunoverStatus"].ToString().Equals("红冲"))
                {
                    base.OpenEFWindowAlert("选中的记录为红冲记录，不能进行红冲处理！", true);
                    return;
                }

                if (this.grid.GetCheckedEIRows()[0]["fdStatus"] != null
                    && (this.grid.GetCheckedEIRows()[0]["fdStatus"].ToString().Equals("已回收")
                    || this.grid.GetCheckedEIRows()[0]["fdStatus"].ToString().Equals("已周转")))
                {
                    base.OpenEFWindowAlert("选中的记录已经回收，不能进行红冲处理！", true);
                    return;
                }
                else if (this.grid.GetCheckedEIRows()[0]["fdStatus"] != null
                    && this.grid.GetCheckedEIRows()[0]["fdStatus"].ToString().Equals("已红冲"))
                {
                    base.OpenEFWindowAlert("选中的记录已经红冲，不能再次进行红冲处理！", true);
                    return;
                }
                else
                {
                    DialogParameters param = this.ModelDialogParameters("Confirm", "确定红冲选中的材料回收记录？");
                    param.Closed = OnDeleteConfirmClosed;
                    EFWindow.Confirm(param);
                }
            }
        }

        /// <summary>
        /// 红冲确认，点击红冲按钮执行事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnDeleteConfirmClosed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == null || !e.DialogResult.Value)
            {
                this.TempButtonOpen(true);//取消后，按钮生效
                return;
            }
            if (e.DialogResult.Value)
            {
                base.Submit(OperationMode.Update, "ZMCMC0", "delete", DataRange.CheckedItems, DoCommitCompleted, null);
            }
        }

        /// <summary>
        /// 执行完红冲操作的回调函数
        /// </summary>
        public override void DoCommitCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo.Status == -1)
            {
                String msg = "插入失败！";
                if (e.EIInfo.GetMsg() != null || e.EIInfo.GetMsg().Equals(""))
                {
                    msg = e.EIInfo.Msg;
                }
                base.OpenEFWindowAlert(msg, true);
                base.TempButtonOpen(true);
            }
            else
            {
                this.grid.Refresh();
                base.TempButtonOpen(true);//按钮生效
            }
        }

        /// <summary>
        /// 新增、修改、查看之前的数据传输
        /// 构造新增页面内容、弹出页面
        /// </summary>
        /// <param name="_eIInfo"></param>
        /// <param name="_title"></param>
        /// <param name="_dialogType"></param>
        /// <param name="_args"></param>
        public override void OpenPopup(EIInfo _eIInfo, string _title, string _dialogType, string _args)
        {
            if (_args.ToLower() == "export")
            {
                this.ExportOpenPopup(_eIInfo, _title, _dialogType, _args);
            }
            else 
            {
                this.ImportOpenPopup(_eIInfo, _title, _dialogType, _args);
            }
        }
        /// <summary>
        /// 发料弹出窗口方法
        /// </summary>
        /// <param name="_eIInfo"></param>
        /// <param name="_title"></param>
        /// <param name="_dialogType"></param>
        /// <param name="_args"></param>
        public void ExportOpenPopup(EIInfo _eIInfo, string _title, string _dialogType, string _args) 
        {
            ExportChildWindow exportWindow = new ExportChildWindow(_dialogType);
            exportWindow.EIDataContext = new Platform.Core.UI.EIDataContext();
            _eIInfo.AddBlock((new RecordBlock()).GetRecordStatus());    //业务类型下拉框
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cs.units"));  //计量单位下拉框
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cm.org"));   //customview取值
            _eIInfo.AddBlock((new RecordBlock()).GetMaterialStatus());
            exportWindow.fdTrunoverStatus.Value = "发料";
            exportWindow.fdBusinessType.Value = "回收";
            exportWindow.fdExportDate.Value = DateTime.Now.ToString("yyyy-MM-dd");
            exportWindow.EIDataContext.Update(_eIInfo);

            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = _title == null ? "" : _title;//设置Title
            _popup.Content = exportWindow;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(OperationWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口
        }
        /// <summary>
        /// 回收料弹出窗口方法
        /// </summary>
        /// <param name="_eIInfo"></param>
        /// <param name="_title"></param>
        /// <param name="_dialogType"></param>
        /// <param name="_args"></param>
        public void ImportOpenPopup(EIInfo _eIInfo, string _title, string _dialogType, string _args)
        {
            ImportChildWindow importWindow = new ImportChildWindow(_dialogType);
            importWindow.EIDataContext = new Platform.Core.UI.EIDataContext();
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cs.units"));//计量单位下拉框
            _eIInfo.AddBlock((new RecordBlock()).GetRecordStatus());//业务类型下拉框
            _eIInfo.AddBlock((new RecordBlock()).GetMaterialStatus());
            importWindow.fdImportNumber.Value = _eIInfo.GetBlock("result").GetRow(0)["fdActualNumber"];
            importWindow.fdTrunoverStatus.Value = "回收";
            importWindow.fdBusinessType.Value = "回收";
            importWindow.fdImportDate.Value = DateTime.Now.ToString("yyyy-MM-dd");
            importWindow.EIDataContext.Update(_eIInfo);

            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = _title == null ? "" : _title;//设置Title
            _popup.Content = importWindow;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(OperationWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口
        }

        /// <summary>
        /// 发料/回收料弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OperationWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                //刷新Grid区域
                this.grid.Refresh();
            }
            //按钮生效
            base.TempButtonOpen(true);
        }
    }
}
